﻿using System;
using System.Collections;
using Col = Column<string, System.Type, int>;
using Guru.Data;
using System.Data;
using Guru.Web.Controls.Telerik;
using Guru.Utilities;
using Guru.Web.UI;
using Telerik.Web.UI;


public partial class FE_ServicesModal : HSMPage
{
    protected int RoomID
    {
        get 
        {
            return int.Parse(SecureQueryString["roomID"]);
        }
    }
    protected int InvoiceID
    {
        get
        {
            return int.Parse(SecureQueryString["invoiceID"]);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.PageType = GuruPageType.Modal;
        this.FixedModalWidth = HSMConstants.DefaultFixedModalWidth;
        if (!IsPostBack)
        {
            this.BindServices();
            this.uxCustomerView.LoadByGuestByInvoice(this.InvoiceID);
        }
    }

    protected void uxServicesGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        GuruParameterCollection parameters = new GuruParameterCollection();       
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceID", this.InvoiceID, DbType.Int32);
        parameters.Add("@RoomID", this.RoomID, DbType.Int32);
        uxServicesGrid.DataSource = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Invoice_GetServices", parameters);

        DataTable dtTotal = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Invoice_GetServices_Total", parameters);
        uxTotalAmount.Text = string.Empty;
        if (dtTotal != null && dtTotal.Rows.Count > 0)
        {
            uxTotalAmount.Text = Formatter.FormatCurrency(dtTotal.Rows[0]["TotalAmount"], this.CurrentLanguage);
        }
    }

    protected void uxServicesGrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            GridDataItem item = e.Item as GridDataItem;
            int id = int.Parse(item.GetDataKeyValue("InvoiceDetailID").ToString());
            GuruParameterCollection parameters = new GuruParameterCollection();
            GuruParameterCollection outParameters = new GuruParameterCollection();
            parameters.AddUserLoggedParams();
            parameters.Add("@RecordID", id, DbType.Int32);

            HSMCommon.HSMBusiness.UpdateData("spa_HSM_Invoice_RemoveService", parameters, out outParameters);
        }
    }

    protected void uxAddService_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.uxServices.SelectedValue) || this.uxNumber.Value<=0) return;
        GuruParameterCollection parameters = new GuruParameterCollection();
        GuruParameterCollection outParameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        parameters.Add("@InvoiceID", this.InvoiceID, DbType.Int32);
        parameters.Add("@RoomID", this.RoomID, DbType.Int32);       
        parameters.Add("@ServiceID", int.Parse(this.uxServices.SelectedValue), DbType.Int32);
        parameters.Add("@Quantity", this.uxNumber.Value, DbType.Double);       
        HSMCommon.HSMBusiness.UpdateData("spa_HSM_Invoice_AddService", parameters, out outParameters);

        this.uxNumber.Text = string.Empty;
        this.uxServices.SelectedIndex = 0;
        this.uxServicesGrid.Rebind();
    }

    protected void BindServices()
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        parameters.AddUserLoggedParams();
        DataTable dtValues = HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Service_GetAll", parameters);
        uxServices.Items.Clear();
        foreach (DataRow dr in dtValues.Rows)
        {           
            GuruComboBoxItem item = new GuruComboBoxItem();
            item.Text = string.Format("{0} - {1}/{2}", dr["ServiceName"].ToSafeString(), dr["Price"].FormatCurrency(((GuruPage)Page).CurrentLanguage), dr["ServiceUnitName"].ToSafeString());
            item.Value = dr["ServiceID"].ToString();
            uxServices.Items.Add(item);            
        }
    }
}
